/* 
 * File:   TerrainExample.cpp
 * Author: aljosa
 * 
 * Created on June 22, 2010, 9:29 PM
 */

#include "TerrainExample.h"

// Subsystems
#include "RenderingSystem.h"



#include <cstdlib>
#include <ctime>
#include <queue>

#include "EventSystem.h"
#include "TranslateEvent.h"
#include "MathUtil.h"

//#include <SDL/SDL_opengl.h>

#include "World.h"

//#include "../Event.h"



using namespace xEngine::Renderer;
using namespace xEngine::CoreSystem::Events;

TerrainExample::TerrainExample() {
}

TerrainExample::~TerrainExample() {
}

// Initialization, destruction
bool TerrainExample::init() {
    // Create game world
    this->world = new World(); // Create world object
    world->init();

    terrain.init();

    this->camera.center = Vector3(0.0f, 0.0f, 0.0f);
    this->camera.eye = Vector3((MAP_X*MAP_SCALE)/2.0f, 250.0f, -(MAP_Z*MAP_SCALE)/2.0f );
   // this->camera.eye = Vector3(0.0f, 5.0f,-30.0f );
    this->camera.up = Vector3(0.0f, 1.0f, 0.0f);

   // gluLookAt(camera.eye.x, camera.eye.y,  camera.eye.z,  camera.center.x, camera.center.y, camera.center.z,  camera.up.x, camera.up.y, camera.up.z   );

  /*  ( (World2D*)world )->create(0.0f, -10.0f, 28, 23); // Create world
    this->setWorld( this->world ); // Set pointer

    // Create game entities
    StaticBody *staticb = new StaticBody(((World2D*)this->world)->worldBox2D);

    Box2DNode *sleft = new Box2DNode(((World2D*)this->world)->worldBox2D);
    sleft->init(b2Vec2(1.0f, 10.0f), b2Vec2( -9.0f, 1.0f));

    Box2DNode *sright = new Box2DNode(((World2D*)this->world)->worldBox2D);
    sright->init(b2Vec2(1.0f, 10.0f), b2Vec2( 9.0f, 1.0f));


    staticb->name = "staticdown";
    sleft->name = "staticleft";
    sright->name = "staticright";


    // Add them to scene graph
    this->world->addChild(staticb);
    this->world->addChild(sleft);
    this->world->addChild(sright);


    // Make Box2D objects appear
    staticb->appear(false);
    sleft->appear(false);
    sright->appear(false);*/



    return true;
}

void TerrainExample::shutDown() {
    // Clean world
    if ( this->world != NULL ) {
        this->world->shutDown();
        delete world;
        world = NULL;
    }
}

// Game loop related
void TerrainExample::draw(double deltaTime) {
    //terrain;


//xEngine::Renderer::RenderingSystem::get()->drawCircle(Vector3(0.0f, 0.0f, -20.0f), 4, 10);
   gluLookAt(camera.eye.x, camera.eye.y,  camera.eye.z,  camera.center.x, camera.center.y, camera.center.z,  camera.up.x, camera.up.y, camera.up.z   );
   xEngine::Renderer::RenderingSystem::get()->drawTerrain(Vector3(0.0f, 0.0f, 0.0f), terrain);
  // this->world->draw(deltaTime);
}

void TerrainExample::update(double deltaTime) {
    // Process events
    //queue<Event*>  *qu = xEngine::CoreSystem::EventSystem::EventSystem::getInstance().getQueue(); // Get event queue
    //EventSystem *events = &EventSystem::getInstance();


    // Loop thorugh events
    /*while ( events->numPending() > 0 ) {
        Event *event = events->popEvent();

        Box2DNode* node = NULL;

        // Handle events
        switch ( event->type )
        {
            default:
                cout<<"Default: " << event->type << endl;

        }
    }*/




    // Update world
    this->world->update(deltaTime);
}

void TerrainExample::animate(double deltaTime) {
}

void TerrainExample::handleInput() {
}
